package com.example.remoteaction.extension.demo.server;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;

import com.example.remoteaction.extension.demo.domain.School;
import com.example.remoteaction.extension.demo.domain.Student;

@Configurable(autowire=Autowire.BY_TYPE)
@Transactional
public class CreateDataService{
	
	@PersistenceContext    
    transient EntityManager entityManager;    
	
	public void init(){
		
		
		School schoolA = new School();
		schoolA.setSchoolName("school A");
		
		entityManager.persist(schoolA);
		
		
		Student studentA1 = new Student();
		studentA1.setName("studentA1");
		studentA1.setAddress("address A1");
		studentA1.setSchool(schoolA);
		
		entityManager.persist(studentA1);
		
		
		
		
		School schoolB = new School();
		schoolB.setSchoolName("school B");
		
		entityManager.persist(schoolB);
		
		
		Student studentB1 = new Student();
		studentB1.setName("studentB1");
		studentB1.setAddress("address B1");
		studentB1.setSchool(schoolB);
		
		Student studentB2 = new Student();
		studentB2.setName("studentB2");
		studentB2.setAddress("address B2");
		studentB2.setSchool(schoolB);
		
		Student studentB3 = new Student();
		studentB3.setName("studentB3");
		studentB3.setAddress("address B3");
		studentB3.setSchool(schoolB);
		
		entityManager.persist(studentB1);
		entityManager.persist(studentB2);
		entityManager.persist(studentB3);
		
		entityManager.flush();
		

	}
	
}
